package com.itheima.controller;

import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.Result;
import com.itheima.domain.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ShangChuanHang
 * @createTime 2022/6/6 17:21
 * @description 购物车模块--控制器
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /*
     * @author ShangChuanHang
     * @createTime 2022/6/6 17:24
     * @desc 获取用户购物车列表数据
     * @param null
     * @return com.itheima.common.Result
     */
    @GetMapping("/list")
    public Result list(){
        //1.调用service,返回业务处理数据结果集
        List<ShoppingCart> shoppingCartList = shoppingCartService.list();
        //2.封装数据结果
        if (shoppingCartList==null){
            shoppingCartList = new ArrayList<>();
        }
        return new Result (Code.OK, MessageConstant.CART_ADD_SUCCESS,shoppingCartList);
    }
    /*
     * @author ShangChuanHang
     * @createTime 2022/6/6 19:20
     * @desc 菜品,套餐,添加购物车
     * @param shoppingCart
     * @return com.itheima.common.Result
     */
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCart shoppingCart){
        //1.调用service,获取业务处理好结果
        ShoppingCart sc = shoppingCartService.add(shoppingCart);
        if(sc==null){
            return new Result(Code.ERROR,MessageConstant.CART_ADD_ERROR);
        }
        //2.封装结果数据
        return new Result(Code.OK,MessageConstant.CART_ADD_SUCCESS,sc);
    }
}
